package com.tiger.weightcheck;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class CheckActivity extends BaseActivity {
	private float[] points=new float[19];
	PreferenceDataUtil util=new PreferenceDataUtil(this);
	private float totalPoint=0;
	private float checkedPotin=0;
	private int subjectNum=1;
	private Button pre,next;
	private TextView subject_tile;
	private RadioGroup subjectrP;
	private RadioButton subject_1,subject_2,subject_3;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.check);
        CommonUtil.setAdView(this, this);
        initView();
    }
    OnCheckedChangeListener listener=new OnCheckedChangeListener(){
		@Override
		public void onCheckedChanged(CompoundButton radio, boolean arg1) {
			// TODO Auto-generated method stub
			switch(radio.getId()){
			case R.id.subject_1:
				if(radio.isChecked()){
					checkedPotin=Float.parseFloat(CheckActivity.this.getResources().getString(CheckActivity.this.getResources().getIdentifier("com.tiger.weightcheck:string/subject_value_"+subjectNum+"_1", null, null)));
				}
				break;
			case R.id.subject_2:
				if(radio.isChecked()){
					checkedPotin=Float.parseFloat(CheckActivity.this.getResources().getString(CheckActivity.this.getResources().getIdentifier("com.tiger.weightcheck:string/subject_value_"+subjectNum+"_1", null, null)));
				}
				break;
			case R.id.subject_3:
				if(radio.isChecked()){
					checkedPotin=Float.parseFloat(CheckActivity.this.getResources().getString(CheckActivity.this.getResources().getIdentifier("com.tiger.weightcheck:string/subject_value_"+subjectNum+"_1", null, null)));
				}
				break;
			}
			points[subjectNum-1]=checkedPotin;
		}

	};
    public void initView(){
    	subject_tile=(TextView)this.findViewById(R.id.subject_title);
    	subject_1=(RadioButton)this.findViewById(R.id.subject_1);
    	subject_2=(RadioButton)this.findViewById(R.id.subject_2);
    	subject_3=(RadioButton)this.findViewById(R.id.subject_3);
    	subjectrP=(RadioGroup)this.findViewById(R.id.radioGroup);
    	setRadioText();
    	subject_1.setOnCheckedChangeListener(listener);
    	subject_2.setOnCheckedChangeListener(listener);
    	subject_3.setOnCheckedChangeListener(listener);
    	
    	pre=(Button)this.findViewById(R.id.preverous);
        pre.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				if(!validChecked()){
					Toast.makeText(CheckActivity.this, "请选择答案", Toast.LENGTH_LONG).show();
					return;
				}
				// TODO Auto-generated method stub
				if(subjectNum==1){
					Toast.makeText(CheckActivity.this, "已经第一题", Toast.LENGTH_LONG).show();
					return;
				}
				subjectNum--;
				setRadioText();
			}
        });
        next=(Button)this.findViewById(R.id.next);
        next.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				if(!validChecked()){
					Toast.makeText(CheckActivity.this, "请选择答案", Toast.LENGTH_LONG).show();
					return;
				}
				// TODO Auto-generated method stub
				if(subjectNum==19){
					for(float point:points){
						totalPoint+=point;
					}
					util.WriteSharedPreferences(util.CHECKRESULT, ""+totalPoint);
					CheckActivity.this.startActivity(new Intent(CheckActivity.this,ResultActivity.class));
					return;
				}
				subjectNum++;
				setRadioText();
			}
        	
        });
        
    }
    private void setRadioText(){
    	subjectrP.clearCheck();
    	subject_1.setChecked(false);
    	subject_2.setChecked(false);
    	subject_3.setChecked(false);
    	subject_tile.setText(this.getResources().getIdentifier("com.tiger.weightcheck:string/subject_title_"+subjectNum, null, null));
    	subject_tile.setText(subjectNum+":"+subject_tile.getText());
    	subject_1.setText(this.getResources().getIdentifier("com.tiger.weightcheck:string/subject_"+subjectNum+"_1", null, null));
    	subject_2.setText(this.getResources().getIdentifier("com.tiger.weightcheck:string/subject_"+subjectNum+"_2", null, null));
    	subject_3.setText(this.getResources().getIdentifier("com.tiger.weightcheck:string/subject_"+subjectNum+"_3", null, null));
    }
    private boolean validChecked(){
    	if(subject_1.isChecked()||subject_2.isChecked()||subject_3.isChecked()){
    		return true;
    	}else{
    		return false;
    	}
    }
}